package com.cn.kabalifemerchants.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.orhanobut.logger.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaolin on 2018/4/2.
 * 功能：  Gson的操作、转bean。转map等。、
 */

public class GsonUtil {


    /**
     * 对象转换成json字符串
     *
     * @param obj
     * @return
     */
    public static String toJson(Object obj) {
        Gson gson = new Gson();
        return gson.toJson(obj);
    }

    /**
     * 将Json数据解析成相应的映射对象
     *
     * @param jsonData JSON
     * @param type     对象
     * @return
     */
    public static <T> T GsonToJsonClass(String jsonData, Class<T> type) {
        // Gson gson = new Gson();
        Gson gson = new GsonBuilder().serializeNulls().create();
        return gson.fromJson(jsonData, type);
    }

    /**
     * json转为map String,object
     *
     * @param jsonStr json字符串
     * @return map
     */
    public static Map<String, Object> jsonToMap(String jsonStr) {
        Map<String, Object> objMap = null;
        Gson gson = new Gson();
        if (gson != null) {
            Type type = new com.google.gson.reflect.TypeToken<Map<String, Object>>() {
            }.getType();
            objMap = gson.fromJson(jsonStr, type);
        }
        return objMap;
    }

    /**
     * map转为json
     *
     * @param maps
     * @return
     */
    public static String mapToJsonStr(Map<String, String> maps) {
        Gson gson = new Gson();
        return gson.toJson(maps);

    }

    /**
     * map转为json
     *
     * @param maps
     * @return
     */
    public static String mapToJsonStrObj(Map<String, Object> maps) {
        Gson gson = new Gson();
        return gson.toJson(maps);

    }

    /**
     * String,String
     *
     * @param jsonStr
     * @return
     */
    public static Map<String, String> jsonToStrMap(String jsonStr) {
        Map<String, String> objMap = null;
        Gson gson = new Gson();
        if (gson != null) {
            Type type = new com.google.gson.reflect.TypeToken<Map<String, String>>() {
            }.getType();
            objMap = gson.fromJson(jsonStr, type);
        }
        return objMap;
    }

    /**
     * List<>转json</>
     *
     * @param list
     * @return
     */
    public static String getJsonData(List<?> list) {
        Gson gson = new Gson();
        return gson.toJson(list);
    }

    public static <T> T getListData(String json, Type list) {
        T result = null;
        Gson gson = new Gson();
        if (gson != null) {
            result = gson.fromJson(json, list);
        }
        return result;
    }


    // 将Json数据解析成相应的映射对象
    public static <T> T fromJson(String jsonData, Class<T> type) {
        Gson gson = new Gson();
        T result = null;
        try {
            result = gson.fromJson(jsonData, type);
        } catch (Exception e) {
            Logger.e("GsonUtil/fromJson: GsonUtil解析Json数据解出错" + e);

        }
        return result;
    }

    //将Json数组解析成相应的映射对象列表
    public static <T> List<T> parseJsonArrayWithGson(String jsonData, Class<T> type) {
        Gson gson = new Gson();
//        Type listType = new TypeToken<ArrayList<T>>(){}.getType();
//        List<T> result = gson.fromJson(jsonData, listType);
//        return result;

        ArrayList<T> mList;
        mList = new ArrayList<>();
        JsonArray array = new JsonParser().parse(jsonData).getAsJsonArray();
        for (final JsonElement elem : array) {
            mList.add(gson.fromJson(elem, type));
        }
        return mList;
    }


    /**
     * 得到json文件中的内容
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        //使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

}
